using System;
using System.Web.UI.Design;
using System.Windows.Forms;
using Microsoft.Web.Design;
using MyDS = MyDataSource.MyDataSource;

namespace MyDesigner
{
	public class MyDesignerImpl : DataSourceDesigner
	{
	    private MyDesignerDSView m_View;
		
		public MyDesignerImpl()
		{
			Logger.Log("MyDesignerImpl .ctor");
		}

		public override void Configure()
		{
			try
			{
				InvokeTransactedChange(base.Component,
					new TransactedChangeCallback(ConfigureDataSourceChangeCallback),
					null, SR.GetString("DataSource_ConfigureTransactionDescription"));
			}
			catch (Exception e)
			{
				Logger.Log(e.ToString());
			}
		}

		private bool ConfigureDataSourceChangeCallback(object context)
		{
			try
			{
				this.SuppressDataSourceEvents();
                MyDS dataSource = (MyDS)this.Component;
                dataSource.TestFunction();
                Random r = new Random();
                dataSource.ConnectionString = "ConnectionString " + r.Next();
                Logger.Log(string.Format("Configure: new connection string - {0}",
                    dataSource.ConnectionString));
                MessageBox.Show(dataSource.ConnectionString,
                                "Connection string",
                                                MessageBoxButtons.OK,
                                                MessageBoxIcon.Information);
				this.OnDataSourceChanged(EventArgs.Empty);
				return true;
			}
			catch(Exception e)
			{
				Logger.Log(e.ToString());
				return false;
			}
			finally
			{
				this.ResumeDataSourceEvents();
			}
		}

        protected override bool DataBindingsEnabled
        {
            get
            {
                return true;
            }
        }

		public override DesignerDataSourceView GetView(string viewName)
		{
			Logger.Log("GetView " + viewName);
			return View;
		}

		public override bool CanConfigure
		{
			get
			{
				Logger.Log("CanConfigure");
				return true;
			}
		}

		public override bool CanRefreshSchema
		{
			get
			{
				Logger.Log("CanRefreshSchema");
				return true;
			}
		}
        
	    protected MyDesignerDSView View
	    {
	        get
	        {
                if(m_View == null)
                    m_View = new MyDesignerDSView(this, String.Empty);
	            return m_View;
	        }
	    }

	    public override string[] GetViewNames()
		{
			Logger.Log("GetViewNames");
			return new string[]{MyDesignerDSView.designerName};
		}

		public override void RefreshSchema(bool preferSilent)
		{
			Logger.Log("RefreshSchema");
		}
	}

    public class MyDesignerDSView : DesignerDataSourceView
    {
        public static readonly string designerName = "MyDesignerDataSourceView";
        public MyDesignerDSView(IDataSourceDesigner owner, string viewName)
            : base(owner, designerName)
        {
        }

        public override System.Collections.IEnumerable GetDesignTimeData(int minimumRows, 
            out bool isSampleData)
        {
            isSampleData = true;
            return MyDataSource.SampleDataHelper.RecordItems;
        }

        public override IDataSourceViewSchema Schema
        {
            get
            {
                return new MyDataSource.RecordItemSchema();
            }
        }

        public override bool CanRetrieveTotalRowCount
        {
            get
            {
                return true;
            }
        }
    }
}
